﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IssueTracker.Business.BO;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for IssueTest
    /// </summary>
    [TestClass]
    public class IssueTest
    {
        
  
        [TestMethod]
        public void TestAddIssue()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();
            
            IssueTrackerService.Issue entity = new IssueTrackerService.Issue();

            List <IssueTrackerService.Project> projectList;

            try
            {
                client.Open();

                projectList = client.ListProjects().ToList();

                if (projectList != null)
                {
                    entity.Id = System.Guid.NewGuid();
                    entity.CreatorId = "testuser";
                    entity.Description = "issue desc";
                    entity.LongDescription = "long desc";
                    entity.ProjectId = projectList.First().Id;
                    entity.Title = "title";

                    IssueTrackerService.Issue result = client.CreateIssue(entity);

                    Assert.IsNotNull(result.Id);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestUpdateIssue()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            List<IssueTrackerService.Issue> list;

            try
            {
                client.Open();
                list = client.ListIssues().ToList();

                if (list != null)
                {
                    IssueTrackerService.Issue entity = list.First();

                    entity.Description = "modified desc";

                    IssueTrackerService.Issue result = client.UpdateIssue(entity);

                    Assert.AreEqual("modified desc", result.Description);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestDeleteIssue()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            System.Guid id;
            List<IssueTrackerService.Issue> list;

            try
            {
                client.Open();
                list = client.ListIssues().ToList();

                if (list != null)
                {
                    IssueTrackerService.Issue entity = list.First();

                    id = entity.Id;

                    client.DeleteIssue(entity);

                    var checkIssue = client.GetIssue(id);

                    Assert.IsNull(checkIssue);
                }
                else
                {
                    Assert.Fail("Issue Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestTaskList()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            IEnumerable<IssueTrackerService.Issue> list;

            try
            {
                client.Open();

                list = client.TaskList("", "");

                Assert.IsNotNull(list);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Source + ": " + ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestTaskListSearch()
        {
            IssueTracker.Business.Entity.Issue issue = new IssueTracker.Business.Entity.Issue();

            issue.Title = "today";

            IssueBO bo = new IssueBO();
            

            var list = bo.SearchIssues(issue);

            Assert.IsNotNull(list);

            Assert.IsTrue(list.Count() > 0);

        }

        [TestMethod]
        public void TestUpdateStatus()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            IEnumerable<IssueTrackerService.Issue> list;

            IssueTrackerService.Issue returnIssue;

            try
            {
                client.Open();

                list = client.TaskList("", "");

                Assert.IsNotNull(list);

                foreach (IssueTrackerService.Issue issue in list)
                {
                    if (issue.CurrentStateName == "Pending Approval")
                    {
                        returnIssue = client.UpdateWorkflowStatus(issue.Id.ToString(), "ValidateIssue");
                        Assert.AreEqual(returnIssue.CurrentStateName, "Pending Solution");
                        break;
                    }
                    
                }

               
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Source + ": " + ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

   
    }
}
